package com.sps.business.service.impl;

import java.util.List;

import com.sps.common.core.domain.entity.SysUser;
import com.sps.common.exception.ServiceException;
import com.sps.common.utils.DateUtils;
import com.sps.common.utils.SecurityUtils;
import com.sps.common.utils.StringUtils;
import com.sps.common.utils.bean.BeanValidators;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sps.business.mapper.JcItemInfoMapper;
import com.sps.business.domain.JcItemInfo;
import com.sps.business.service.IJcItemInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 物料信息Service业务层处理
 * 
 * @author 王超
 * @date 2024-12-10
 */
@Service
public class JcItemInfoServiceImpl implements IJcItemInfoService 
{
    private static final Logger log = LoggerFactory.getLogger(JcItemInfoServiceImpl.class);
    @Autowired
    private JcItemInfoMapper jcItemInfoMapper;

    /**
     * 查询物料信息
     * 
     * @param id 物料信息主键
     * @return 物料信息
     */
    @Override
    public JcItemInfo selectJcItemInfoById(Long id)
    {
        return jcItemInfoMapper.selectJcItemInfoById(id);
    }

    /**
     * 查询物料信息列表
     * 
     * @param jcItemInfo 物料信息
     * @return 物料信息
     */
    @Override
    public List<JcItemInfo> selectJcItemInfoList(JcItemInfo jcItemInfo)
    {
        return jcItemInfoMapper.selectJcItemInfoList(jcItemInfo);
    }

    /**
     * 新增物料信息
     * 
     * @param jcItemInfo 物料信息
     * @return 结果
     */
    @Override
    public int insertJcItemInfo(JcItemInfo jcItemInfo)
    {
        jcItemInfo.setCreateTime(DateUtils.getNowDate());
        jcItemInfo.setCaeateUser(SecurityUtils.getUsername());
        JcItemInfo jcItemInfo1 = jcItemInfoMapper.selectJcItemInfoByItemCode(jcItemInfo.getItemCode());
        if(jcItemInfo1!=null){
            throw new ServiceException("物料号:"+jcItemInfo.getItemCode()+"已存在!");
        }
        return jcItemInfoMapper.insertJcItemInfo(jcItemInfo);
    }

    /**
     * 修改物料信息
     * 
     * @param jcItemInfo 物料信息
     * @return 结果
     */
    @Override
    public int updateJcItemInfo(JcItemInfo jcItemInfo)
    {
        return jcItemInfoMapper.updateJcItemInfo(jcItemInfo);
    }

    /**
     * 批量删除物料信息
     * 
     * @param ids 需要删除的物料信息主键
     * @return 结果
     */
    @Override
    public int deleteJcItemInfoByIds(Long[] ids)
    {
        return jcItemInfoMapper.deleteJcItemInfoByIds(ids);
    }

    /**
     * 删除物料信息信息
     * 
     * @param id 物料信息主键
     * @return 结果
     */
    @Override
    public int deleteJcItemInfoById(Long id)
    {
        return jcItemInfoMapper.deleteJcItemInfoById(id);
    }

    @Transactional
    @Override
    public String importItem(List<JcItemInfo> itemList, boolean updateSupport, String operName) {
        String message = "";
        if (StringUtils.isNull(itemList) || itemList.size() == 0)
        {
            throw new ServiceException("导入物料数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int i = 0;
        for (JcItemInfo item : itemList)
        {
            //校验已存在的物料
            JcItemInfo jcItemInfo = jcItemInfoMapper.selectJcItemInfoByItemCode(item.getItemCode());
            if(jcItemInfo!=null){
                throw new ServiceException("物料号:"+item.getItemCode()+"已存在");
            }
            item.setCreateTime(DateUtils.getNowDate());
            item.setCaeateUser(SecurityUtils.getLoginUser().getUsername());
             i = jcItemInfoMapper.insertJcItemInfo(item);
            successNum++;
        }
        if(i>0){
            message = "成功导入"+successNum+"条数据";
        }else{
            message="数据导入失败";
        }
        return message;
    }

    @Override
    public JcItemInfo selectJcItemInfoByItemCode(String itemCode) {
        return jcItemInfoMapper.selectJcItemInfoByItemCode(itemCode);
    }

    @Override
    public JcItemInfo selectJcItemInfoByLableCode(String lableCode) {
        return jcItemInfoMapper.selectJcItemInfoByLableCode(lableCode);
    }
}
